using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWaves.Web.Controls
{
    public class Image : ImageBase
    {     
        public int Width
        {
            get { return (int)(ViewState["Width"] ?? 0); }
            set { ViewState["Width"] = value; }
        }

        public int Height
        {
            get { return (int)(ViewState["Height"] ?? 0); }
            set { ViewState["Height"] = value; }
        }

        public bool RenderDimensionsWhenKnown
        {
            get { return (bool)(this.ViewState["RenderDimensionsWhenKnown"] ?? true); }
            set { this.ViewState["RenderDimensionsWhenKnown"] = value; }
        }

        protected override void RenderImage(string imageUrl, HtmlTextWriter writer)
        {
            if (this.RenderDimensionsWhenKnown)
            {
                if (this.ImageDimensionsAreKnown)
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Width, new Unit(this.Width, UnitType.Pixel).ToString(CultureInfo.InvariantCulture));
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Height, new Unit(this.Height, UnitType.Pixel).ToString(CultureInfo.InvariantCulture));
                }
            }

            this.RenderImageTag(imageUrl, this.Transformation, this.CustomTransformation, this.Width, this.Height, writer);
        }

        protected virtual bool ImageDimensionsAreKnown
        {
            get
            {
                return this.Transformation == ImageTransformationType.ScaleToFill ||
                       this.Transformation == ImageTransformationType.Stretch;
            }
        }
    }
}